import std.stdio;

void main( char[][] args ) {
   test_closure();
}

void delegate() test1() {
    int a = 7;
    return delegate() { writefln ( a + 3); }; // construccion anonima de un delegado
}
 
void delegate() test2() {
    int a = 20;
    void aux() { writefln ( a + 5); } // funcion anidada
    return &aux;  // otra forma de construir un delegado
}
 
void test_closure() {
    auto dg = test1();
    dg();    // =10   // ok, test1.a is in a closure and still exists
 
    dg = test2();
    dg();    // =25   // ok, test2.a is in a closure and still exists
}